//
//  UserRegViewController.m
//  WangLottery
//
//  Created by 黄修勇 on 12-5-13.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UserRegViewController.h"
#import "RegexKitLite.h"

#import "WWRequestAPI+WWAccountAPI.h"

@interface UserRegViewController ()

@end

@implementation UserRegViewController

@synthesize regTableView=_regTableView;
@synthesize userNameField = _userNameField;
@synthesize phoneTextField=_phoneTextField;
@synthesize pswTextField=_pswTextField;
@synthesize verPswTextField=_verPswTextField;


-(void)dealloc{

    [_regTableView release];
    [_pswTextField release];
    [_phoneTextField release];
    [_verPswTextField release];
    [super dealloc];
}
//注册协议
-(IBAction)registRules:(id)sender
{
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.96 alpha:1]];
    
    self.navigationItem.title=@"新用户注册";
    
    _regTableView.contentInset=UIEdgeInsetsMake(20.0f, 0.0f, 0.0f, 0.0f);
    
    //[self changeBackNavItem];
    [backItemButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
}

-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(IBAction)regDone:(id)sender{
    
    //键盘消失
    [self.phoneTextField resignFirstResponder];
    [self.pswTextField resignFirstResponder];
    [self.verPswTextField resignFirstResponder];

    if (_phoneTextField.text==nil||[_phoneTextField.text isEqualToString:@""]) {
        [self showAlertViewWithTitle:@"提示" msg:@"手机号码不能为空"];
        return;
    }
    
    if (_pswTextField.text==nil||[_pswTextField.text isEqualToString:@""]) {
        [self showAlertViewWithTitle:@"提示" msg:@"密码不能为空"];
        return;
    }
    if (_pswTextField.text.length<8) {
        [self showAlertViewWithTitle:@"提示" msg:@"密码不能少于八位!"];
        return;
    }
    
    if (![_pswTextField.text isEqualToString:_verPswTextField.text]) {
        [self showAlertViewWithTitle:@"提示" msg:@"两次输入的密码不一致!"];
        return;
    }
    
    if(!agree.isOn)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"您必须同意注册协议" message:@"您还没有同意注册协议，不能注册" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    
    //注册,如果返回值为多少则成功注册，并跳转到用户信息界面
    returnTag = [[WWRequestAPI currentRequestAPI] registWithName:_userNameField.text phone:_phoneTextField.text psw:_pswTextField.text target:self];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //键盘消失
    [self.phoneTextField resignFirstResponder];
    [self.pswTextField resignFirstResponder];
    [self.verPswTextField resignFirstResponder];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


#pragma mark -

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 4;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //因为数量比较少不用重用
    
    UITableViewCell *cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"regi"] autorelease];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.backgroundColor=[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.4f];
    
    UIFont *font=[UIFont systemFontOfSize:14.0f];
    UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(20.0f, 45.0f/2-31.0f/2, 60.0f, 31.0f)];
    titleLabel.backgroundColor=[UIColor clearColor];
    titleLabel.font=font;
    [cell.contentView addSubview:titleLabel];
    
    
    UITextField *textFiled=[[UITextField alloc]initWithFrame:CGRectMake(90.0f, titleLabel.frame.origin.y+8.0f, 200.0f, 31.0f)];
    textFiled.delegate=self;
    textFiled.font=font;
    textFiled.clearButtonMode=UITextFieldViewModeWhileEditing;
    textFiled.returnKeyType=UIReturnKeyDone;
    [cell.contentView addSubview:textFiled];
    
    
    switch (indexPath.row) {
        case 0:
            titleLabel.text = @"用户名";
            textFiled.placeholder = @"请输入用户名";
            self.userNameField = textFiled;
            break;
        case 1:
            titleLabel.text=@"手机号";
            textFiled.placeholder=@"用于大奖兑换";
            self.phoneTextField=textFiled;
            textFiled.keyboardType=UIKeyboardTypeNumberPad;
            break;
        case 2:
            titleLabel.text=@"密码";
            textFiled.placeholder=@"输入密码,8－20位数字或字母";
            self.pswTextField=textFiled;
            textFiled.secureTextEntry=YES;
            break;
        case 3:
            titleLabel.text=@"确认密码";
            textFiled.placeholder=@"再次输入密码";
            self.verPswTextField=textFiled;
            textFiled.returnKeyType=UIReturnKeyDone;
            textFiled.secureTextEntry=YES;
            break;
        default:
            break;
    }
    
    
    [titleLabel release];
    [textFiled release];
    
    
    return cell;
    
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
    return YES;
}

-(void)requesResult:(NSDictionary *)dic{
    
    NSLog(@"dic %@",dic);
    if([[dic objectForKey:@"result"] isEqualToString:@"0"])
    {
        NSLog(@"ok");
        LoginSuccessViewController *success = [[LoginSuccessViewController alloc] initWithNibName:@"LoginSuccessViewController" bundle:nil];
        [self.navigationController pushViewController:success animated:YES];
        [success release];
    }
    else
    {
        NSString *info = [dic objectForKey:@"result"];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"失败" message:info delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

@end
